﻿#include "Earth3DView.h"
#include <amk3/EarthWrapper.h>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QQuickItem>
#include <QThread>
#include <QString>
#include "QMLInterface.h"
#include <QMessageBox>
#include "MainService.h"
#include <amk3/ScreenDisplay.h>
//#include <5.4.2/QtQuick/private/qquickwindow_p.h>

//class UpdateThread : public QThread
//{
//    // QThread interface
//protected:
//    void run(){
//        EarthWrapper::intance()->createMapNode();
//    }
//};

Earth3DView::Earth3DView()
{
    _stereo = false;
}

void Earth3DView::init()
{
    OsgGraphicView::init();
    EarthWrapperOptions options;
    options.viewer = _viewer;
    EarthWrapper::instance()->init(options);
    //emit this->singlInted();

	//MainService::instance()->setMidNightStyle(true);

    ScreenDisplay::instance()->setEnabled(false);
}

void Earth3DView::render()
{
    osg::ref_ptr<osg::Camera> camera = _viewer->getCamera();
    camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);

    if(_stereo){

        EarthWrapper::instance()->renderStereo(1);
        this->openglContext()->makeCurrent(this);
        camera->setClearMask(GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
        EarthWrapper::instance()->renderStereo(2);

        //QQuickWindowPrivate*d = QQuickWindowPrivate::get(this);
        //d->context->renderNextFrame(d->renderer,0);

        glDrawBuffer(GL_BACK_LEFT);
    }else{
        EarthWrapper::instance()->render();
    }

    this->openglContext()->makeCurrent(this);
}
#include <QQmlContext>
void Earth3DView::registerQMLProperty(const QString &name, const QVariant& value)
{
    rootContext()->setContextProperty(name, value);
}


void Earth3DView::slotStereo()
{
    this->_stereo = !_stereo;
}

void Earth3DView::slotGotoHenan()
{
    osgEarth::Viewpoint vp( "Henan",113,33.6,0, 0, -70, 6e5 );
    EarthWrapper::instance()->getManipulator()->setViewpoint(vp,3.0);
}

void Earth3DView::slotGotoHome()
{
    osgEarth::Viewpoint vp( "Home",113,33.6,0, 0, -90, 10e6 );
    EarthWrapper::instance()->getManipulator()->setViewpoint(vp,3.0);
}

void Earth3DView::slotSceneInited()
{
    //emit this->signalCityRanking();
    //osgEarth::Viewpoint vp( "Henan",113,33.6,0, 0, -70, 6e5 );
    //EarthWrapper::intance()->getManipulator()->setViewpoint(vp,5.0);
}

void Earth3DView::mousePressEvent(QMouseEvent *event)
{
    OsgGraphicView::mousePressEvent(event);
    //emit this->signalCloseAnnoInfo();
    //if(event->isAccepted()) return;
    //this->rootObject()->setFocus(true);
    //emit closeChildMenu();

}

void Earth3DView::mouseReleaseEvent(QMouseEvent *event)
{
    OsgGraphicView::mouseReleaseEvent(event);
}

void Earth3DView::mouseDoubleClickEvent(QMouseEvent *event)
{
    OsgGraphicView::mouseDoubleClickEvent(event);
}

void Earth3DView::mouseMoveEvent(QMouseEvent *event)
{
    OsgGraphicView::mouseMoveEvent(event);
}
#include "MainService.h"
void Earth3DView::keyPressEvent(QKeyEvent *e)
{
	switch (e->key()) {
	case Qt::Key_0:
	{
		osgEarth::Viewpoint vp = EarthWrapper::instance()->getManipulator()->getViewpoint();
		std::string str = vp.getConfig().toJSON(true);
		QMessageBox::information(0, "", QString::fromStdString(str));
		break;
	}
	case Qt::Key_T:
	{
		MainService::instance()->showCyberThreat();
		break;
	}

}
return QQuickView::keyPressEvent(e);
}

